package com.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Map;

//@SessionAttributes({"name"})  name会自动添加进session中
@Controller
public class MyController {

	@RequestMapping("/hello")
	public String hello(){
		return "hello";
	}

	/**
	 *
	 * 正确连接：http://localhost:8088/paramAndHeader?username=chen&age=2
	 * 错误连接：http://localhost:8088/paramAndHeader?username=chen&age=10
	 * @return
	 */
	@RequestMapping(value = "/paramAndHeader",
			params = {"username","age!=10"}
			,headers = {"Accept-Language=zh-CN,zh;q=0.9,zh-TW;q=0.8"})
	public String hh(){
		return "hello";
	}
	@RequestMapping(value = "/paramAndHeader2",
			params = {"username","age!=10"}
			,headers = {"Accept-Language=en-US,zh;q=0.9,zh-TW;q=0.8"})
	public String hh1(){
		return "hello";
	}

	@ResponseBody
	@RequestMapping("/headers")
	public String getHeaders(@RequestHeader("Accept-Language") String language,
	                         @CookieValue("JSESSIONID") String cookie){
		return language+","+cookie;
	}


	@RequestMapping(value = "/accounts", method = RequestMethod.POST)
	public String ha(RedirectAttributes redirectAttrs) {
		redirectAttrs.addAttribute("id", 1)
				.addFlashAttribute("message", "Account created!");
		return "redirect:/accounts/{id}";
	}

	@RequestMapping(value = "/accounts/{id}", method = RequestMethod.GET)
	public String handle(@PathVariable("id") String id
						 ,Model model) {
		System.out.println(id);
		boolean message = model.containsAttribute("message");
		if (message){
			Object message1 = model.asMap().get("message");
		}
		return "hello";
	}


	@RequestMapping("/helloSession")
	public String sessionTest(Map map){
		map.put("name","chen");
		return "hello";
	}
}
